<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
function replace_smile($str)
{
    $str = preg_replace('/:Smile-(.*?)\:/i', '<img src="' . base_url('public/face/\1.png') . '" />', $str);
    return $str;
}

function get_user_name($username, $uname)
{
    if ($uname) {
        return $uname;
    } else {
        return $username;
    }
}

function get_user_name_hide($username, $uname)
{
    if ($uname) {
        $s = $uname;
    } else {
        $s = $username;
    }
    return mb_substr($s, 0, 4);
}

function float_num($num)
{
    if (is_numeric($num)) {
        return floatval(sprintf('%.2f', $num));
    }
}

function url_abc($text)
{
    $search = "/^[A-Za-z]+$/";
    if (preg_match($search, $text)) {
        return (true);
    } else {
        return (false);
    }
}

function cut_dress($a, $b)
{
    if ($a == $b) {
        return $a;
    } else {
        return $a . $b;
    }
}

function yincang_num($str, $num = 3, $n = 3)
{
    $str    = $str;
    $resstr = substr_replace($str, '****', $num, $n);
    return $resstr;
}

function month_everyday($year, $month)
{
    $j          = date('t', strtotime("" . $year . "-" . $month . "-01")); //获取当前月份天数
    $start_time = strtotime(date('' . $year . '-' . $month . '-01'), strtotime("2017-08-01"));  //获取本月第一天时间戳
    $array      = [];
    for ($i = 0; $i < $j; $i++) {
        $array[] = date('Y-m-d', $start_time + $i * 86400); //每隔一天赋值给数组
    }
    return $array;
}

function fox_ch_num($money = 0, $is_round = true, $int_unit = '元', $zheng = '')
{
    $chs     = [0, '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'];
    $uni     = ['', '拾', '佰', '仟'];
    $dec_uni = ['角', '分'];
    $exp     = ['', '万', '亿'];
    $res     = '';
    // 以 元为单位分割
    $parts = explode('.', $money, 2);
    $int   = isset ($parts [0]) ? strval($parts [0]) : 0;
    $dec   = isset ($parts [1]) ? strval($parts [1]) : '';
    // 处理小数点
    $dec_len = strlen($dec);
    if (isset ($parts [1]) && $dec_len > 2) {
        $dec = $is_round ? substr(strrchr(strval(round(floatval("0." . $dec), 2)), '.'), 1) : substr($parts [1], 0, 2);
    }
    // number= 0.00时，直接返回 0
    if (empty ($int) && empty ($dec)) {
        return '零';
    }

    // 整数部分 从右向左
    for ($i = strlen($int) - 1, $t = 0; $i >= 0; $t++) {
        $str = '';
        // 每4字为一段进行转化
        for ($j = 0; $j < 4 && $i >= 0; $j++, $i--) {
            $u   = $int{$i} > 0 ? $uni [$j] : '';
            $str = $chs [$int{$i}] . $u . $str;
        }
        $str = rtrim($str, '0');
        $str = preg_replace("/0+/", "零", $str);
        $u2  = $str != '' ? $exp [$t] : '';
        $res = $str . $u2 . $res;
    }
    $dec = rtrim($dec, '0');
    // 小数部分 从左向右
    if (!empty ($dec)) {
        $res .= $int_unit;
        $cnt = strlen($dec);
        for ($i = 0; $i < $cnt; $i++) {
            $u   = $dec{$i} > 0 ? $dec_uni [$i] : ''; // 非0的数字后面添加单位
            $res .= $chs [$dec{$i}] . $u;
        }
        if ($cnt == 1) $res .= $zheng;
        $res = rtrim($res, '0'); // 去掉末尾的0
        $res = preg_replace("/0+/", "零", $res); // 替换多个连续的0
    } else {
        $res .= $int_unit . $zheng;
    }
    return $res;
}

function daxie_month($number)
{
    $number = substr($number, 0, 2);
    $arr    = ["零", "一", "二", "三", "四", "五", "六", "七", "八", "九"];
    if (strlen($number) == 1) {
        $result = $arr[$number];
    } else {
        if ($number == 10) {
            $result = "十";
        } else {
            if ($number < 20) {
                $result = "十";
            } else {
                $result = $arr[substr($number, 0, 1)] . "十";
            }
            if (substr($number, 1, 1) != "0") {
                $result .= $arr[substr($number, 1, 1)];
            }
        }
    }
    return $result . "月";
}

//规则下的URL安全加密及解密
function fox_fanurl($url)
{
    $url = str_replace('+', 'XXXXX', str_replace(')', 'YYYYY', str_replace('(', 'ZZZZZ', str_replace(' ', 'KKKKK', $url))));
    return $url;
}

function fox_deurl($url)
{
    $url = str_replace('ZZZZZ', '(', str_replace('YYYYY', ')', str_replace('KKKKK', ' ', str_replace('XXXXX', '+', $url))));
    return $url;
}

//链接加
function fox_links($str = '')
{
    if ($str == '' or !preg_match('/(http|www\.|@)/i', $str)) {
        return $str;
    }
    $lines    = explode("\n", $str);
    $new_text = '';
    while (list($k, $l) = each($lines)) {
        // replace links:
        $l        = preg_replace("/([ \t]|^)www\./i", "\\1http://www.", $l);
        $l        = preg_replace("/([ \t]|^)ftp\./i", "\\1ftp://ftp.", $l);
        $l        = preg_replace("/(http:\/\/[^ )\r\n!]+)/i", "<a target=\"\\_blank\" href=\"\\1\">\\1</a>", $l);
        $l        = preg_replace("/(https:\/\/[^ )\r\n!]+)/i", "<a target=\"\\_blank\" href=\"\\1\">\\1</a>", $l);
        $l        = preg_replace("/(ftp:\/\/[^ )\r\n!]+)/i", "<a target=\"\\_blank\" href=\"\\1\">\\1</a>", $l);
        $l        = preg_replace("/([-a-z0-9_]+(\.[_a-z0-9-]+)*@([a-z0-9-]+(\.[a-z0-9-]+)+))/i", "<a target=\"\\_blank\" href=\"mailto:\\1\">\\1</a>", $l);
        $new_text .= $l . "\n";
    }
    return $new_text;
}

function getExcel($fileName, $headArr, $data, $ex, $dates, $savePath)
{
    if (empty($data) || !is_array($data)) {
        die("无数据可导出，请检查是否正确！");
    }
    if (empty($fileName)) {
        exit;
    }
    $date = $dates;
    if ($ex == '2003') {
        $fileName .= "_{$date}.xls";
    } elseif ($ex == '2007') {
        $fileName .= "_{$date}.xlsx";
    }
    require_once(APPPATH . 'third_party/PHPExcel.php');
    require_once(APPPATH . 'third_party/phpExcel/Writer/Excel2007.php');
    require_once(APPPATH . 'third_party/phpExcel/Writer/Excel5.php');
    require_once(APPPATH . 'third_party/PHPExcel/IOFactory.php');

    //创建新的PHPExcel对象
    $objPHPExcel = new PHPExcel();
    $objProps    = $objPHPExcel->getProperties();

    //设置表头
    $key = ord("A");
    foreach ($headArr as $v) {
        $colum = chr($key);
        $objPHPExcel->setActiveSheetIndex(0)->setCellValue($colum . '1', $v);
        $key += 1;
    }

    $column      = 2;
    $objActSheet = $objPHPExcel->getActiveSheet();
    foreach ($data as $key => $rows) { //行写入
        $span = ord("A");
        foreach ($rows as $keyName => $value) {// 列写入
            $j = chr($span);
            $objActSheet->setCellValue($j . $column, $value);
            $span++;
        }
        $column++;
    }
    $objActSheet->getColumnDimension('a')->setWidth(15);//改变此处设置的长度数值
    $objActSheet->getColumnDimension('f')->setWidth(20);//改变此处设置的长度数值
    $objActSheet->getColumnDimension('h')->setWidth(20);//改变此处设置的长度数值
    $objActSheet->getColumnDimension('i')->setWidth(20);//改变此处设置的长度数值

    $fileName = iconv("utf-8", "gb2312", $fileName);
    //重命名表
    $objPHPExcel->getActiveSheet()->setTitle('Simple');
    //设置活动单指数到第一个表,所以Excel打开这是第一个表
    $objPHPExcel->setActiveSheetIndex(0);
    //将输出重定向到一个客户端web浏览器(Excel2007)
    if ($ex == '2003') {
        //header('Content-Type: application/vnd.ms-excel; charset=UTF-8');
        //header("Content-Disposition: attachment; filename=\"$fileName\"");
        //header('Cache-Control: max-age=0');
        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
    } elseif ($ex == '2007') {
        //header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet; charset=UTF-8');
        //header("Content-Disposition: attachment; filename=\"$fileName\"");
        //header('Cache-Control: max-age=0');
        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
    }
    $savename = $savePath . $fileName;
    //if(!empty($_GET['excel'])){
    //	$objWriter->save('php://output'); //文件通过浏览器下载
    //}else{
    $objWriter->save($savename); //脚本方式运行，保存在当前目录
    //}
    return $savename;
    exit;
}

function fox_chines_str($str)
{
    preg_match_all('/[\x{4e00}-\x{9fff}]|[\，]|[\。]|[\！]|[\？]+/u', $str, $matches);
    $str = join('', $matches[0]);
    return $str;
}

/**
 * 截取中英混排字符串
 * @param (string) $string
 * @param (int) $length
 * @param (string) $dot
 * @param (string) $charset
 */
function fox_substr($string, $length, $dot = '...', $charset = 'utf-8')
{
    $slen = strlen($string);
    if ($slen <= $length) {
        return $string;
    }
    if (function_exists('mb_substr')) {
        return mb_substr($string, 0, $length, $charset) . $dot;
    }
    $strcut = '';
    if (strtolower($charset) == 'utf-8') {
        $n = $tn = $noc = 0;
        while ($n < $slen) {
            $t = ord($string[$n]);
            if ($t == 9 || $t == 10 || (32 <= $t && $t <= 126)) {
                $tn = 1;
                $n++;
                $noc++;
            } elseif (194 <= $t && $t <= 223) {
                $tn  = 2;
                $n   += 2;
                $noc += 1;
            } elseif (224 <= $t && $t < 239) {
                $tn  = 3;
                $n   += 3;
                $noc += 1;
            } elseif (240 <= $t && $t <= 247) {
                $tn  = 4;
                $n   += 4;
                $noc += 1;
            } elseif (248 <= $t && $t <= 251) {
                $tn  = 5;
                $n   += 5;
                $noc += 1;
            } elseif ($t == 252 || $t == 253) {
                $tn  = 6;
                $n   += 6;
                $noc += 1;
            } else {
                $n++;
            }
            if ($noc >= $length) {
                break;
            }
        }
        if ($noc > $length) {
            $n -= $tn;
        }
        $strcut = substr($string, 0, $n);
    } else {
        for ($i = 0; $i < $length; $i++) {
            $strcut .= ord($string[$i]) > 127 ? $string[$i] . $string[++$i] : $string[$i];
        }
    }

    return $strcut . $dot;
}

/**
 * 清除HTML标记
 *
 * @param    string $str
 * @return  string
 */
function cleanhtml($str)
{
    $str = strip_tags($str);
    $str = htmlspecialchars($str);
    $str = preg_replace("/\s+/", " ", $str); //过滤多余回车
    $str = preg_replace("/ /", "", $str);
    $str = preg_replace("/&nbsp;/", "", $str);
    $str = preg_replace("/　/", "", $str);
    $str = preg_replace("/\r\n/", "", $str);
    $str = str_replace(chr(13), "", $str);
    $str = str_replace(chr(10), "", $str);
    $str = str_replace(chr(9), "", $str);
    return $str;
}

//过滤空白
function trimall($str)
{
    $qian = [" ", "　", "\t", "\n", "\r"];
    return str_replace($qian, '', $str);
}

function check_auth()
{
    $url = 'http://www.kuaiwww.com/authorize/check_auth/' . get_domain();
    if (function_exists('file_get_contents')) {
        $data = file_get_contents($url);
    } else {
        $ch      = curl_init();
        $timeout = 5;
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
        $data = curl_exec($ch);
        curl_close($ch);
    }
    $data = json_decode($data);
    return $data->product;
}

//长度范围
function e_range($str, $min, $max)
{
    $len = strlen($str);
    if ($len >= $min && $len < $max) {
        return true;
    }
    return false;
}

//长度范围(中英文混合)
function mix_range($str, $min, $max)
{
    $len = (strlen($str) + mb_strlen($str, 'utf8')) / 2;
    if ($len >= $min && $len <= $max) {
        return true;
    }
    return false;
}

//QQ号
function is_qq($str)
{
    if (preg_match('/[1-9][0-9]{4,10}/', $str)) {
        return true;
    }
    return false;
}

//手机号码验证
function checkMobile($mobilephone)
{
    if (preg_match("/^13[0-9]{1}[0-9]{8}$|15[0-9]{1}[0-9]{8}$|18[0-9]{1}[0-9]{8}$/", $mobilephone)) {
        return true;
    } else {
        return false;
    }
}

//邮箱验证类
function validateEmail($email)
{
    $pattern = "/\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/";  //[ch]定义验证email正则表达式
    if (preg_match($pattern, $email, $counts)) {
        return true;
    } else {
        return false;
    }
}

//sms接口相关结束，发送短信
function SendShouji($a, $b, $c, $d, $e)
{
    $target = "http://106.ihuyi.cn/webservice/sms.php?method=Submit";
    //替换成自己的测试账号,参数顺序和wenservice对应
    $post_data = "account=" . $a . "&password=" . $b . "&mobile=" . $c . "&content=" . urlencode($d);
    $gets      = ihuyi_Post($post_data, $target);
    $gets_arr  = xml_to_array($gets);
    if ($gets_arr['SubmitResult']['code'] == 2) {
        return true;
    } else {
        return false;
    }
}

function xml_to_array($xml)
{
    $reg = "/<(\w+)[^>]*>([\\x00-\\xFF]*)<\\/\\1>/";
    if (preg_match_all($reg, $xml, $matches)) {
        $count = count($matches[0]);
        for ($i = 0; $i < $count; $i++) {
            $subxml = $matches[2][$i];
            $key    = $matches[1][$i];
            if (preg_match($reg, $subxml)) {
                $arr[$key] = xml_to_array($subxml);
            } else {
                $arr[$key] = $subxml;
            }
        }
    }
    return @$arr;
}

//sms接口相关
function ihuyi_Post($curlPost, $url)
{
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_HEADER, false);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_NOBODY, true);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);
    $return_str = curl_exec($curl);
    curl_close($curl);
    return $return_str;
}

function get_domain($url = '')
{
    $host = $url ? $url : @$_SERVER[HTTP_HOST];
    $host = strtolower($host);
    if (strpos($host, '/') !== false) {
        $parse = @parse_url($host);
        $host  = $parse['host'];
    }
    $topleveldomaindb = ['com', 'edu', 'gov', 'int', 'mil', 'net', 'org', 'biz', 'info', 'pro', 'name', 'museum', 'coop', 'aero', 'xxx', 'idv', 'mobi', 'cc', 'me', 'cn', 'tv', 'in', 'hk', 'de', 'us', 'tw'];
    $str              = '';
    foreach ($topleveldomaindb as $v) {
        $str .= ($str ? '|' : '') . $v;
    }
    $matchstr = "[^\.]+\.(?:(" . $str . ")|\w{2}|((" . $str . ")\.\w{2}))$";
    if (preg_match("/" . $matchstr . "/ies", $host, $matchs)) {
        $domain = $matchs['0'];
    } else {
        $domain = $host;
    }
    return $domain;
}

function filter_check($str)
{

    $pattern = "/<pre[^>]*>(.*?)<\/pre>/si";
    preg_match_all($pattern, $str, $matches);
    $str = htmlspecialchars_decode($str);
    $str = stripslashes($str);
    if ($matches[1]) {
        foreach ($matches[1] as $v) {
            $replace[] = addslashes(htmlspecialchars(trim($v)));
        }
        $str = str_replace($matches[1], $replace, $str);
    } else {
        $str = strip_tags($str, "<img> <pre> <a> <font> <span> <em>");
    }
    $str = nl2br($str);

    return $str;
}

//过滤
function filter_code($str)
{
    $str     = htmlspecialchars_decode($str);
    $pattern = "/<pre[^>]*>(.*?)<\/pre>/si";
    preg_match_all($pattern, $str, $matches);
    if ($matches[1]) {
        foreach ($matches[1] as $v) {
            $replace = trim(htmlentities($v));
            $str     = str_replace($v, $replace, $str);
        }
        $str = strip_tags($str, "<img> <pre> <a> <font> <span> <em> <p> <b>");
    } else {
        $str = strip_tags($str, "<img> <pre> <a> <font> <span> <em> <p> <b>");
        $str = trim(nl2br($str));
    }
    return $str;
}

function br2nl($text)
{
    return preg_replace('/<br\\s*?\/??>/i', '', $text);
}

function xss_clean($input_str)
{
    $return_str = str_replace(['<', '>', "'", '"', ')', '('], ['&lt;', '&gt;', '&apos;', '&#x22;', '&#x29;', '&#x28;'], $input_str);
    $return_str = str_ireplace('%3Cscript', '', $return_str);
    return $return_str;
}

function xss_clean3($str)
{

    if (isset($str)) {
        $str = trim($str);  //清理空格
        $str = strip_tags($str);   //过滤html标签
        $str = htmlspecialchars($str);   //将字符内容转化为html实体
        $str = addslashes($str);
        return $str;
    }
}

function xss_clean1($data)
{
    // Fix &entity\n;
    $data = str_replace(['&amp;', '&lt;', '&gt;'], ['&amp;amp;', '&amp;lt;', '&amp;gt;'], $data);
    $data = preg_replace('/(&#*\w+)[\x00-\x20]+;/u', '$1;', $data);
    $data = preg_replace('/(&#x*[0-9A-F]+);*/iu', '$1;', $data);
    $data = html_entity_decode($data, ENT_COMPAT, 'UTF-8');

    // Remove any attribute starting with "on" or xmlns
    $data = preg_replace('#(<[^>]+?[\x00-\x20"\'])(?:on|xmlns)[^>]*+>#iu', '$1>', $data);

    // Remove javascript: and vbscript: protocols
    $data = preg_replace('#([a-z]*)[\x00-\x20]*=[\x00-\x20]*([`\'"]*)[\x00-\x20]*j[\x00-\x20]*a[\x00-\x20]*v[\x00-\x20]*a[\x00-\x20]*s[\x00-\x20]*c[\x00-\x20]*r[\x00-\x20]*i[\x00-\x20]*p[\x00-\x20]*t[\x00-\x20]*:#iu', '$1=$2nojavascript...', $data);
    $data = preg_replace('#([a-z]*)[\x00-\x20]*=([\'"]*)[\x00-\x20]*v[\x00-\x20]*b[\x00-\x20]*s[\x00-\x20]*c[\x00-\x20]*r[\x00-\x20]*i[\x00-\x20]*p[\x00-\x20]*t[\x00-\x20]*:#iu', '$1=$2novbscript...', $data);
    $data = preg_replace('#([a-z]*)[\x00-\x20]*=([\'"]*)[\x00-\x20]*-moz-binding[\x00-\x20]*:#u', '$1=$2nomozbinding...', $data);

    // Only works in IE: <span style="width: expression(alert('Ping!'));"></span>
    $data = preg_replace('#(<[^>]+?)style[\x00-\x20]*=[\x00-\x20]*[`\'"]*.*?expression[\x00-\x20]*\([^>]*+>#i', '$1>', $data);
    $data = preg_replace('#(<[^>]+?)style[\x00-\x20]*=[\x00-\x20]*[`\'"]*.*?behaviour[\x00-\x20]*\([^>]*+>#i', '$1>', $data);
    $data = preg_replace('#(<[^>]+?)style[\x00-\x20]*=[\x00-\x20]*[`\'"]*.*?s[\x00-\x20]*c[\x00-\x20]*r[\x00-\x20]*i[\x00-\x20]*p[\x00-\x20]*t[\x00-\x20]*:*[^>]*+>#iu', '$1>', $data);

    // Remove namespaced elements (we do not need them)
    $data = preg_replace('#</*\w+:\w[^>]*+>#i', '', $data);

    do {
        // Remove really unwanted tags
        $old_data = $data;
        $data     = preg_replace('#</*(?:applet|b(?:ase|gsound|link)|embed|frame(?:set)?|i(?:frame|layer)|l(?:ayer|ink)|meta|object|s(?:cript|tyle)|title|xml)[^>]*+>#i', '', $data);
    } while ($old_data !== $data);

    // we are done...
    return $data;
}

//生成随机数
function key_randkey($length)
{
    $pattern = '123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
    $key     = '';
    for ($i = 0; $i < $length; $i++) {
        $key .= $pattern{mt_rand(0, 60)};    //生成php随机数
    }
    return $key;
}

function randkey($length)
{
    $pattern = '1234567890abcdefghijklmnopqrstuvwxyz$#&^@!';
    $key     = '';
    for ($i = 0; $i < $length; $i++) {
        $key .= $pattern{mt_rand(0, 35)};    //生成php随机数
    }
    return $key;
}

function csrf_hidden()
{
    $ci   = &get_instance();
    $name = $ci->security->get_csrf_token_name();
    $val  = $ci->security->get_csrf_hash();
    echo "<input type=\"hidden\" id=\"$name\" class=\"$name\" name=\"$name\" value=\"$val\" />";
}

function csrf_name()
{
    $ci   = &get_instance();
    $name = $ci->security->get_csrf_token_name();
    return $name;
}

function csrf_value()
{
    $ci  = &get_instance();
    $val = $ci->security->get_csrf_hash();
    return $val;
}

function get_url_content($url)
{
    if (function_exists('file_get_contents')) {
        return file_get_contents($url);
    } elseif (function_exists('curl_init')) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        return curl_exec($ch);
    }
}

/*生成盐salt*/

function get_salt($length = -6)
{
    return substr(uniqid(rand()), $length);
}

/*生成密码*/

function password_dohash($password, $salt)
{
    $salt = $salt ? $salt : get_salt();
    return md5(md5($password) . $salt);
}

/*返回信息*/
function show_message($message = '', $url = '', $status = 2, $heading = '提示信息', $time = 1800)
{
    include FCPATH . 'themes/errors/html/show_message.php';
    exit;
}

/*返回信息*/
function shop_msg($message = '', $url = '', $status = 2, $heading = '提示信息', $text = '', $time = 1800)
{
    include FCPATH . 'themes/shop/show_message.php';
    exit;
}

/*返回信息*/
function vip_msg($message = '', $url = '', $status = 2, $heading = '提示信息', $text = '', $time = 1800)
{
    include FCPATH . 'themes/wallet/show_message.php';
    exit;
}

////获得本地真实IP
function get_onlineip()
{
    $ip = 'bad ip';
    if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
        return is_ip($_SERVER['HTTP_CLIENT_IP']) ? $_SERVER['HTTP_CLIENT_IP'] : $ip;
    } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
        return is_ip($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $ip;
    } else {
        return is_ip($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : $ip;
    }
}

function is_ip($str)
{
    $ip = explode('.', $str);
    for ($i = 0; $i < count($ip); $i++) {
        if ($ip[$i] > 255) {
            return false;
        }
    }
    return preg_match('/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/', $str);
}

/*补全代码*/
function closetags($html)
{
    // 不需要补全的标签
    $arr_single_tags = ['meta', 'img', 'br', 'link', 'area'];
    // 匹配开始标签
    preg_match_all('#<([a-z]+)(?: .*)?(?<![/|/ ])>#iU', $html, $result);
    $openedtags = $result[1];
    // 匹配关闭标签
    preg_match_all('#</([a-z]+)>#iU', $html, $result);
    $closedtags = $result[1];
    // 计算关闭开启标签数量，如果相同就返回html数据
    $len_opened = count($openedtags);
    if (count($closedtags) == $len_opened) {
        return $html;
    }
    // 把排序数组，将最后一个开启的标签放在最前面
    $openedtags = array_reverse($openedtags);
    // 遍历开启标签数组
    for ($i = 0; $i < $len_opened; $i++) {
        // 如果需要补全的标签
        if (!in_array($openedtags[$i], $arr_single_tags)) {
            // 如果这个标签不在关闭的标签中
            if (!in_array($openedtags[$i], $closedtags)) {
                // 直接补全闭合标签
                $html .= '</' . $openedtags[$i] . '>';
            } else {
                unset($closedtags[array_search($openedtags[$i], $closedtags)]);
            }
        }
    }
    return $html;
}

function strip_url_tags($str)
{
    $str = preg_replace("/<a[^>]*href=[^>]*>|<\/[^a]*a[^>]*>/i", "\\2", $str);
    return $str;
}

function get_tree_array(&$data, $parentId = 0)
{
    $category = [];
    foreach ($data as $key => $value) {
        if ($value['pid'] == $parentId) {
            unset($data[$key]);
            $value['child'] = category($data, $value['id']);
            $category[]     = $value;
        }
    }
    return $category;
}

function get_tree(&$data, $parentId = 0)
{
    global $str;
    $str .= '<ul>';
    foreach ($data as $key => $value) {
        if ($value['pid'] == $parentId) {
            unset($data[$key]);
            $str .= "<li>|--<a href='/'>" . $value['name'] . '</a></li>';
            get_tree($data, $value['id']);
        }
    }
    $str .= '</ul>';
    return $str;
}

/**
 * $string 明文或密文
 * $operation 加密ENCODE或解密DECODE
 * $key 密钥
 * $expiry 密钥有效期
 */
function authcode($string, $operation = 'DECODE', $key = '', $expiry = 0)
{
    // 动态密匙长度，相同的明文会生成不同密文就是依靠动态密匙
    // 加入随机密钥，可以令密文无任何规律，即便是原文和密钥完全相同，加密结果也会每次不同，增大破解难度。
    // 取值越大，密文变动规律越大，密文变化 = 16 的 $ckey_length 次方
    // 当此值为 0 时，则不产生随机密钥
    $ckey_length = 4;

    // 密匙
    $key = md5($key ? $key : $this->config->item('encryption_key'));

    // 密匙a会参与加解密
    $keya = md5(substr($key, 0, 16));
    // 密匙b会用来做数据完整性验证
    $keyb = md5(substr($key, 16, 16));
    // 密匙c用于变化生成的密文
    $keyc = $ckey_length ? ($operation == 'DECODE' ? substr($string, 0, $ckey_length) : substr(md5(microtime()), -$ckey_length)) : '';
    // 参与运算的密匙
    $cryptkey   = $keya . md5($keya . $keyc);
    $key_length = strlen($cryptkey);
    // 明文，前10位用来保存时间戳，解密时验证数据有效性，10到26位用来保存$keyb(密匙b)，解密时会通过这个密匙验证数据完整性
    // 如果是解码的话，会从第$ckey_length位开始，因为密文前$ckey_length位保存 动态密匙，以保证解密正确
    $string        = $operation == 'DECODE' ? base64_decode(substr($string, $ckey_length)) : sprintf('%010d', $expiry ? $expiry + time() : 0) . substr(md5($string . $keyb), 0, 16) . $string;
    $string_length = strlen($string);
    $result        = '';
    $box           = range(0, 255);
    $rndkey        = [];
    // 产生密匙簿
    for ($i = 0; $i <= 255; $i++) {
        $rndkey[$i] = ord($cryptkey[$i % $key_length]);
    }
    // 用固定的算法，打乱密匙簿，增加随机性，好像很复杂，实际上并不会增加密文的强度
    for ($j = $i = 0; $i < 256; $i++) {
        $j       = ($j + $box[$i] + $rndkey[$i]) % 256;
        $tmp     = $box[$i];
        $box[$i] = $box[$j];
        $box[$j] = $tmp;
    }
    // 核心加解密部分
    for ($a = $j = $i = 0; $i < $string_length; $i++) {
        $a       = ($a + 1) % 256;
        $j       = ($j + $box[$a]) % 256;
        $tmp     = $box[$a];
        $box[$a] = $box[$j];
        $box[$j] = $tmp;
        // 从密匙簿得出密匙进行异或，再转成字符
        $result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));
    }
    if ($operation == 'DECODE') {
        // substr($result, 0, 10) == 0 验证数据有效性
        // substr($result, 0, 10) - time() > 0 验证数据有效性
        // substr($result, 10, 16) == substr(md5(substr($result, 26).$keyb), 0, 16) 验证数据完整性
        // 验证数据有效性，请看未加密明文的格式
        if ((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) && substr($result, 10, 16) == substr(md5(substr($result, 26) . $keyb), 0, 16)) {
            return substr($result, 26);
        } else {
            return '';
        }
    } else {
        // 把动态密匙保存在密文里，这也是为什么同样的明文，生产不同密文后能解密的原因
        // 因为加密后的密文可能是一些特殊字符，复制过程可能会丢失，所以用base64编码
        $result = str_replace('=', '', base64_encode($result));
        //$result = str_replace('+', '', $result);
        return $keyc . $result;
    }
}

//生成友好时间形式
function friendly_date($from)
{
    static $now = null;
    $now == null && $now = time();
    !is_numeric($from) && $from = strtotime($from);
    $seconds = $now - $from;
    $minutes = floor($seconds / 60);
    $hours   = floor($seconds / 3600);
    $day     = round((strtotime(date('Y-m-d', $now)) - strtotime(date('Y-m-d', $from))) / 86400);
    if ($seconds == 0) {
        return '刚刚';
    }
    if (($seconds >= 0) && ($seconds <= 60)) {
        return "{$seconds}秒前";
    }
    if (($minutes >= 0) && ($minutes <= 60)) {
        return "{$minutes}分钟前";
    }
    if (($hours >= 0) && ($hours <= 24)) {
        return "{$hours}小时前";
    }
    if ((date('Y') - date('Y', $from)) > 0) {
        return date('Y-m-d', $from);
    }

    switch ($day) {
        case 0:
            return date('今天H:i', $from);
            break;

        case 1:
            return date('昨天H:i', $from);
            break;

        default:
            //$day += 1;
            return "{$day} 天前";
            break;
    }
}

function str_len($str, $charset = 'utf-8')
{
    if ($charset == 'utf-8') $str = iconv('utf-8', 'gb2312', $str);
    $num   = strlen($str);
    $cnNum = 0;
    for ($i = 0; $i < $num; $i++) {
        if (ord(substr($str, $i + 1, 1)) > 127) {
            $cnNum++;
            $i++;
        }
    }
    $enNum  = $num - ($cnNum * 2);
    $number = ($enNum / 2) + $cnNum;
    return ceil($number);
}

if (!function_exists('array_column')) {
    function array_column($input, $columnKey, $indexKey = null)
    {
        $columnKeyIsNumber = (is_numeric($columnKey)) ? true : false;
        $indexKeyIsNull    = (is_null($indexKey)) ? true : false;
        $indexKeyIsNumber  = (is_numeric($indexKey)) ? true : false;
        $result            = [];
        foreach ((array)$input as $key => $row) {
            if ($columnKeyIsNumber) {
                $tmp = array_slice($row, $columnKey, 1);
                $tmp = (is_array($tmp) && !empty($tmp)) ? current($tmp) : null;
            } else {
                $tmp = isset($row[$columnKey]) ? $row[$columnKey] : null;
            }
            if (!$indexKeyIsNull) {
                if ($indexKeyIsNumber) {
                    $key = array_slice($row, $indexKey, 1);
                    $key = (is_array($key) && !empty($key)) ? current($key) : null;
                    $key = is_null($key) ? 0 : $key;
                } else {
                    $key = isset($row[$indexKey]) ? $row[$indexKey] : 0;
                }
            }
            $result[$key] = $tmp;
        }
        return $result;
    }
}

function is_today($time)
{
    $date  = date('Y-m-d', $time);
    $today = date('Y-m-d');
    if ($date == $today) {
        return true;
    } else {
        return false;
    }
}

//php获取中文字符拼音首字母
function getFirstCharter($str)
{
    if (empty($str)) {
        return '';
    }
    $fchar = ord($str{0});
    if ($fchar >= ord('A') && $fchar <= ord('z')) return strtoupper($str{0});
    $s1  = iconv('UTF-8', 'gb2312', $str);
    $s2  = iconv('gb2312', 'UTF-8', $s1);
    $s   = $s2 == $str ? $s1 : $str;
    $asc = ord($s{0}) * 256 + ord($s{1}) - 65536;
    if ($asc >= -20319 && $asc <= -20284) return 'A';
    if ($asc >= -20283 && $asc <= -19776) return 'B';
    if ($asc >= -19775 && $asc <= -19219) return 'C';
    if ($asc >= -19218 && $asc <= -18711) return 'D';
    if ($asc >= -18710 && $asc <= -18527) return 'E';
    if ($asc >= -18526 && $asc <= -18240) return 'F';
    if ($asc >= -18239 && $asc <= -17923) return 'G';
    if ($asc >= -17922 && $asc <= -17418) return 'H';
    if ($asc >= -17417 && $asc <= -16475) return 'J';
    if ($asc >= -16474 && $asc <= -16213) return 'K';
    if ($asc >= -16212 && $asc <= -15641) return 'L';
    if ($asc >= -15640 && $asc <= -15166) return 'M';
    if ($asc >= -15165 && $asc <= -14923) return 'N';
    if ($asc >= -14922 && $asc <= -14915) return 'O';
    if ($asc >= -14914 && $asc <= -14631) return 'P';
    if ($asc >= -14630 && $asc <= -14150) return 'Q';
    if ($asc >= -14149 && $asc <= -14091) return 'R';
    if ($asc >= -14090 && $asc <= -13319) return 'S';
    if ($asc >= -13318 && $asc <= -12839) return 'T';
    if ($asc >= -12838 && $asc <= -12557) return 'W';
    if ($asc >= -12556 && $asc <= -11848) return 'X';
    if ($asc >= -11847 && $asc <= -11056) return 'Y';
    if ($asc >= -11055 && $asc <= -10247) return 'Z';
    return null;
}

function Pinyin($_String, $_Code = 'UTF8')
{ //GBK页面可改为gb2312，其他随意填写为UTF8
    $_DataKey    = "a|ai|an|ang|ao|ba|bai|ban|bang|bao|bei|ben|beng|bi|bian|biao|bie|bin|bing|bo|bu|ca|cai|can|cang|cao|ce|ceng|cha" .
        "|chai|chan|chang|chao|che|chen|cheng|chi|chong|chou|chu|chuai|chuan|chuang|chui|chun|chuo|ci|cong|cou|cu|" .
        "cuan|cui|cun|cuo|da|dai|dan|dang|dao|de|deng|di|dian|diao|die|ding|diu|dong|dou|du|duan|dui|dun|duo|e|en|er" .
        "|fa|fan|fang|fei|fen|feng|fo|fou|fu|ga|gai|gan|gang|gao|ge|gei|gen|geng|gong|gou|gu|gua|guai|guan|guang|gui" .
        "|gun|guo|ha|hai|han|hang|hao|he|hei|hen|heng|hong|hou|hu|hua|huai|huan|huang|hui|hun|huo|ji|jia|jian|jiang" .
        "|jiao|jie|jin|jing|jiong|jiu|ju|juan|jue|jun|ka|kai|kan|kang|kao|ke|ken|keng|kong|kou|ku|kua|kuai|kuan|kuang" .
        "|kui|kun|kuo|la|lai|lan|lang|lao|le|lei|leng|li|lia|lian|liang|liao|lie|lin|ling|liu|long|lou|lu|lv|luan|lue" .
        "|lun|luo|ma|mai|man|mang|mao|me|mei|men|meng|mi|mian|miao|mie|min|ming|miu|mo|mou|mu|na|nai|nan|nang|nao|ne" .
        "|nei|nen|neng|ni|nian|niang|niao|nie|nin|ning|niu|nong|nu|nv|nuan|nue|nuo|o|ou|pa|pai|pan|pang|pao|pei|pen" .
        "|peng|pi|pian|piao|pie|pin|ping|po|pu|qi|qia|qian|qiang|qiao|qie|qin|qing|qiong|qiu|qu|quan|que|qun|ran|rang" .
        "|rao|re|ren|reng|ri|rong|rou|ru|ruan|rui|run|ruo|sa|sai|san|sang|sao|se|sen|seng|sha|shai|shan|shang|shao|" .
        "she|shen|sheng|shi|shou|shu|shua|shuai|shuan|shuang|shui|shun|shuo|si|song|sou|su|suan|sui|sun|suo|ta|tai|" .
        "tan|tang|tao|te|teng|ti|tian|tiao|tie|ting|tong|tou|tu|tuan|tui|tun|tuo|wa|wai|wan|wang|wei|wen|weng|wo|wu" .
        "|xi|xia|xian|xiang|xiao|xie|xin|xing|xiong|xiu|xu|xuan|xue|xun|ya|yan|yang|yao|ye|yi|yin|ying|yo|yong|you" .
        "|yu|yuan|yue|yun|za|zai|zan|zang|zao|ze|zei|zen|zeng|zha|zhai|zhan|zhang|zhao|zhe|zhen|zheng|zhi|zhong|" .
        "zhou|zhu|zhua|zhuai|zhuan|zhuang|zhui|zhun|zhuo|zi|zong|zou|zu|zuan|zui|zun|zuo";
    $_DataValue  = "-20319|-20317|-20304|-20295|-20292|-20283|-20265|-20257|-20242|-20230|-20051|-20036|-20032|-20026|-20002|-19990" .
        "|-19986|-19982|-19976|-19805|-19784|-19775|-19774|-19763|-19756|-19751|-19746|-19741|-19739|-19728|-19725" .
        "|-19715|-19540|-19531|-19525|-19515|-19500|-19484|-19479|-19467|-19289|-19288|-19281|-19275|-19270|-19263" .
        "|-19261|-19249|-19243|-19242|-19238|-19235|-19227|-19224|-19218|-19212|-19038|-19023|-19018|-19006|-19003" .
        "|-18996|-18977|-18961|-18952|-18783|-18774|-18773|-18763|-18756|-18741|-18735|-18731|-18722|-18710|-18697" .
        "|-18696|-18526|-18518|-18501|-18490|-18478|-18463|-18448|-18447|-18446|-18239|-18237|-18231|-18220|-18211" .
        "|-18201|-18184|-18183|-18181|-18012|-17997|-17988|-17970|-17964|-17961|-17950|-17947|-17931|-17928|-17922" .
        "|-17759|-17752|-17733|-17730|-17721|-17703|-17701|-17697|-17692|-17683|-17676|-17496|-17487|-17482|-17468" .
        "|-17454|-17433|-17427|-17417|-17202|-17185|-16983|-16970|-16942|-16915|-16733|-16708|-16706|-16689|-16664" .
        "|-16657|-16647|-16474|-16470|-16465|-16459|-16452|-16448|-16433|-16429|-16427|-16423|-16419|-16412|-16407" .
        "|-16403|-16401|-16393|-16220|-16216|-16212|-16205|-16202|-16187|-16180|-16171|-16169|-16158|-16155|-15959" .
        "|-15958|-15944|-15933|-15920|-15915|-15903|-15889|-15878|-15707|-15701|-15681|-15667|-15661|-15659|-15652" .
        "|-15640|-15631|-15625|-15454|-15448|-15436|-15435|-15419|-15416|-15408|-15394|-15385|-15377|-15375|-15369" .
        "|-15363|-15362|-15183|-15180|-15165|-15158|-15153|-15150|-15149|-15144|-15143|-15141|-15140|-15139|-15128" .
        "|-15121|-15119|-15117|-15110|-15109|-14941|-14937|-14933|-14930|-14929|-14928|-14926|-14922|-14921|-14914" .
        "|-14908|-14902|-14894|-14889|-14882|-14873|-14871|-14857|-14678|-14674|-14670|-14668|-14663|-14654|-14645" .
        "|-14630|-14594|-14429|-14407|-14399|-14384|-14379|-14368|-14355|-14353|-14345|-14170|-14159|-14151|-14149" .
        "|-14145|-14140|-14137|-14135|-14125|-14123|-14122|-14112|-14109|-14099|-14097|-14094|-14092|-14090|-14087" .
        "|-14083|-13917|-13914|-13910|-13907|-13906|-13905|-13896|-13894|-13878|-13870|-13859|-13847|-13831|-13658" .
        "|-13611|-13601|-13406|-13404|-13400|-13398|-13395|-13391|-13387|-13383|-13367|-13359|-13356|-13343|-13340" .
        "|-13329|-13326|-13318|-13147|-13138|-13120|-13107|-13096|-13095|-13091|-13076|-13068|-13063|-13060|-12888" .
        "|-12875|-12871|-12860|-12858|-12852|-12849|-12838|-12831|-12829|-12812|-12802|-12607|-12597|-12594|-12585" .
        "|-12556|-12359|-12346|-12320|-12300|-12120|-12099|-12089|-12074|-12067|-12058|-12039|-11867|-11861|-11847" .
        "|-11831|-11798|-11781|-11604|-11589|-11536|-11358|-11340|-11339|-11324|-11303|-11097|-11077|-11067|-11055" .
        "|-11052|-11045|-11041|-11038|-11024|-11020|-11019|-11018|-11014|-10838|-10832|-10815|-10800|-10790|-10780" .
        "|-10764|-10587|-10544|-10533|-10519|-10331|-10329|-10328|-10322|-10315|-10309|-10307|-10296|-10281|-10274" .
        "|-10270|-10262|-10260|-10256|-10254";
    $_TDataKey   = explode('|', $_DataKey);
    $_TDataValue = explode('|', $_DataValue);
    $_Data       = array_combine($_TDataKey, $_TDataValue);
    arsort($_Data);
    reset($_Data);
    if ($_Code != 'gb2312') $_String = _U2_Utf8_Gb($_String);
    $_Res = '';
    for ($i = 0; $i < strlen($_String); $i++) {
        $_P = ord(substr($_String, $i, 1));
        if ($_P > 160) {
            $_Q = ord(substr($_String, ++$i, 1));
            $_P = $_P * 256 + $_Q - 65536;
        }
        $_Res .= _Pinyin($_P, $_Data);
    }
    return preg_replace("/[^a-z0-9]*/", '', $_Res);
}

function _Pinyin($_Num, $_Data)
{
    if ($_Num > 0 && $_Num < 160) {
        return chr($_Num);
    } elseif ($_Num < -20319 || $_Num > -10247) {
        return '';
    } else {
        foreach ($_Data as $k => $v) {
            if ($v <= $_Num) break;
        }
        return $k;
    }
}

function _U2_Utf8_Gb($_C)
{
    $_String = '';
    if ($_C < 0x80) {
        $_String .= $_C;
    } elseif ($_C < 0x800) {
        $_String .= chr(0xC0 | $_C >> 6);
        $_String .= chr(0x80 | $_C & 0x3F);
    } elseif ($_C < 0x10000) {
        $_String .= chr(0xE0 | $_C >> 12);
        $_String .= chr(0x80 | $_C >> 6 & 0x3F);
        $_String .= chr(0x80 | $_C & 0x3F);
    } elseif ($_C < 0x200000) {
        $_String .= chr(0xF0 | $_C >> 18);
        $_String .= chr(0x80 | $_C >> 12 & 0x3F);
        $_String .= chr(0x80 | $_C >> 6 & 0x3F);
        $_String .= chr(0x80 | $_C & 0x3F);
    }
    return iconv('UTF-8', 'GB2312', $_String);
}

function array_unique_fb($array2D)
{
    foreach ($array2D as $v) {
        $v      = join(",", $v); //降维,也可以用implode,将一维数组转换为用逗号连接的字符串
        $temp[] = $v;
    }
    $temp = array_unique($temp);    //去掉重复的字符串,也就是重复的一维数组
    foreach ($temp as $k => $v) {
        $temp[$k] = explode(",", $v);   //再将拆开的数组重新组装
    }
    return $temp;
}

function assoc_unique($arr, $key)
{
    $tmp_arr = [];
    foreach ($arr as $k => $v) {
        if (in_array($v[$key], $tmp_arr))//搜索$v[$key]是否在$tmp_arr数组中存在，若存在返回true
        {
            unset($arr[$k]);
        } else {
            $tmp_arr[] = $v[$key];
        }
    }
    sort($arr); //sort函数对数组进行排序
    return $arr;
}

function shuffle_assoc($list)
{
    if (!is_array($list)) return $list;

    $keys = array_keys($list);
    shuffle($keys);
    $random = [];
    foreach ($keys as $key)
        $random[$key] = shuffle_assoc($list[$key]);
    return $random;
}

function party_time($start_time, $end_time, $area = '')
{
    if ($start_time < time() && $end_time < time()) {
        return '<span class="btn btn-warning ' . $area . '">已经结束</span>';
    }
    if ($start_time < time() && $end_time >= time()) {
        return '<span class="btn btn-success ' . $area . '">正在进行</span>';
    }
    if ($start_time >= time() && $end_time >= time()) {
        return '<span class="btn btn-info ' . $area . '">即将开始</span>';
    }
}

function users_sex_biao($sex)
{
    if ($sex == 0) {
        return '--';
    }
    if ($sex == 1) {
        return '<span class="text-blue"><i class="fa fa-mars-stroke-v" aria-hidden="true"></i></span>';
    }
    if ($sex == 2) {
        return '<span class="text-pink"><i class="fa fa-venus" aria-hidden="true"></i></span>';
    }
}

function unique_arr($array2D, $stkeep = false, $ndformat = true)
{
    // 判断是否保留一级数组键 (一级数组键可以为非数字)
    if ($stkeep) $stArr = array_keys($array2D);
    // 判断是否保留二级数组键 (所有二级数组键必须相同)
    if ($ndformat) $ndArr = array_keys(end($array2D));
    //降维,也可以用implode,将一维数组转换为用逗号连接的字符串
    foreach ($array2D as $v) {
        $v      = join(",", $v);
        $temp[] = $v;
    }
    //去掉重复的字符串,也就是重复的一维数组
    $temp = array_unique($temp);
    //再将拆开的数组重新组装
    foreach ($temp as $k => $v) {
        if ($stkeep) $k = $stArr[$k];
        if ($ndformat) {
            $tempArr = explode(",", $v);
            foreach ($tempArr as $ndkey => $ndval) $output[$k][$ndArr[$ndkey]] = $ndval;
        } else $output[$k] = explode(",", $v);
    }
    return $output;
}

function copy_dir($src, $dst)
{
    $dir = opendir($src);
    @mkdir($dst);
    while (false !== ($file = readdir($dir))) {
        if (($file != '.') && ($file != '..')) {
            if (is_dir($src . '/' . $file)) {
                copy_dir($src . '/' . $file, $dst . '/' . $file);
                continue;
            } else {
                copy($src . '/' . $file, $dst . '/' . $file);
            }
        }
    }
    closedir($dir);
}

function deldir($dir)
{
    //先删除目录下的文件：
    $dh = opendir($dir);
    while ($file = readdir($dh)) {
        if ($file != "." && $file != "..") {
            $fullpath = $dir . "/" . $file;
            if (!is_dir($fullpath)) {
                unlink($fullpath);
            } else {
                deldir($fullpath);
            }
        }
    }

    closedir($dh);
    //删除当前文件夹：
    if (rmdir($dir)) {
        return true;
    } else {
        return false;
    }
}

function CheckSubstrs($substrs, $text)
{
    foreach ($substrs as $substr)
        if (false !== strpos($text, $substr)) {
            return true;
        }
    return false;
}

function isMobile()
{
    $useragent               = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';
    $useragent_commentsblock = preg_match('|\(.*?\)|', $useragent, $matches) > 0 ? $matches[0] : '';

    $mobile_os_list    = ['Google Wireless Transcoder', 'Windows CE', 'WindowsCE', 'Symbian', 'Android', 'armv6l', 'armv5', 'Mobile', 'CentOS', 'mowser', 'AvantGo', 'Opera Mobi', 'J2ME/MIDP', 'Smartphone', 'Go.Web', 'Palm', 'iPAQ'];
    $mobile_token_list = ['Profile/MIDP', 'Configuration/CLDC-', '160×160', '176×220', '240×240', '240×320', '320×240', 'UP.Browser', 'UP.Link', 'SymbianOS', 'PalmOS', 'PocketPC', 'SonyEricsson', 'Nokia', 'BlackBerry', 'Vodafone', 'BenQ', 'Novarra-Vision', 'Iris', 'NetFront', 'HTC_', 'Xda_', 'SAMSUNG-SGH', 'Wapaka', 'DoCoMo', 'iPhone', 'iPod'];

    $found_mobile = CheckSubstrs($mobile_os_list, $useragent_commentsblock) ||
        CheckSubstrs($mobile_token_list, $useragent);

    if ($found_mobile) {
        return true;
    } else {
        return false;
    }
}

function get_words_tags_arr($title)
{
    require_once APPPATH . 'third_party/words/pscws4.class.php';
    $pscws = new PSCWS4();
    $pscws->set_dict(APPPATH . 'third_party/words/scws/dict.utf8.xdb');
    $pscws->set_rule(APPPATH . 'third_party/words/scws/rules.utf8.ini');
    $pscws->set_ignore(true);
    $pscws->send_text($title);
    $words = $pscws->get_tops(5);
    $tags  = [];
    foreach ($words as $val) {
        $tags[] = $val['word'];
    }
    $pscws->close();
    return $tags;
}

function sctonum($num, $double = 5)
{
    if (false !== stripos($num, "e")) {
        $a = explode("e", strtolower($num));
        return bcmul($a[0], bcpow(10, $a[1], $double), $double);
    }
}

/**
 * 创建订单号
 * @return string
 */
function create_order_no($start = 0, $num = 9)
{
    $order_number = date('Ymd') . substr(implode(null, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
    return substr($order_number, $start, $num);
}

/**
 *计算某个经纬度的周围某段距离的正方形的四个点
 *
 * @param lng float 经度
 * @param lat float 纬度
 * @param distance float 该点所在圆的半径，该圆与此正方形内切，默认值为0.5千米
 * @return array 正方形的四个点的经纬度坐标
 */
function returnSquarePoint($lng, $lat, $distance = 0.5)
{
    $EARTH_RADIUS = '6378.137';
    $dlng         = 2 * asin(sin($distance / (2 * $EARTH_RADIUS)) / cos(deg2rad($lat)));
    $dlng         = rad2deg($dlng);

    $dlat = $distance / $EARTH_RADIUS;
    $dlat = rad2deg($dlat);

    return [
        'left-top'     => ['lat' => $lat + $dlat, 'lng' => $lng - $dlng],
        'right-top'    => ['lat' => $lat + $dlat, 'lng' => $lng + $dlng],
        'left-bottom'  => ['lat' => $lat - $dlat, 'lng' => $lng - $dlng],
        'right-bottom' => ['lat' => $lat - $dlat, 'lng' => $lng + $dlng],
    ];
}

/**
 *求两个已知经纬度之间的距离,单位为千米
 * @param lng1,lng2 经度
 * @param lat1,lat2 纬度
 * @return float 距离，单位千米
 **/
function fox_distance($lng1, $lat1, $lng2, $lat2, $decimal = 2)//根据经纬度计算距离
{
    //将角度转为弧度
    $radLat1 = deg2rad($lat1);
    $radLat2 = deg2rad($lat2);
    $radLng1 = deg2rad($lng1);
    $radLng2 = deg2rad($lng2);
    $a       = $radLat1 - $radLat2;//两纬度之差,纬度<90
    $b       = $radLng1 - $radLng2;//两经度之差纬度<180
    $s       = 2 * asin(sqrt(pow(sin($a / 2), 2) + cos($radLat1) * cos($radLat2) * pow(sin($b / 2), 2))) * 6378.137;
    $ss      = explode(".", $s);
    return '距离我' . $ss[0] . '.' . substr($ss[1], 0, 2) . '公里';
}

function get_words_keywords_str($content)
{
    require_once APPPATH . 'third_party/words/phpanalysis.class.php';
    PhpAnalysis::$loadInit = false;
    $pa                    = new PhpAnalysis('utf-8', 'utf-8', false);
    $pa->LoadDict();
    $pa->SetSource($content);
    $pa->StartAnalysis(false);
    $tags = $pa->GetFinallyResult();
    return $tags;
}

function pp($bool = false)
{
    list($callee) = debug_backtrace();
    $arguments      = func_get_args();
    $totalArguments = count($arguments);

    echo "<fieldset class='dump'>" . PHP_EOL .
        "<legend>{$callee['file']} @ line: {$callee['line']}</legend>" . PHP_EOL .
        '<pre>';

    $i = 0;
    foreach ($arguments as $argument) {
        echo '<br /><strong>Debug #' . (++$i) . " of {$totalArguments}</strong>: ";

        if (!empty($argument)
            && (is_array($argument) || is_object($argument))
        ) {
            print_r($argument);
        } else {
            var_dump($argument);
        }
    }

    echo '</pre>' . PHP_EOL .
        '</fieldset>' . PHP_EOL;
    if (!$bool) {

    }
}

if (!function_exists('getallheaders')) {
    function getallheaders()
    {
        foreach ($_SERVER as $name => $value) {
            if (substr($name, 0, 5) == 'HTTP_') {
                $headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
            }
        }
        return $headers;
    }
}
/**
 * 百分比分割
 * @param $div
 * @param $total
 * @return array
 */
function get_gcs_transaction_ratio($div)
{

    $total = 100;

    $area = 8; //各份数间允许的最大差值

    $average = round($total / $div);
    $sum     = 0;
    $result  = array_fill(1, $div, 0);

    for ($i = 1; $i < $div; $i++) {
        //根据已产生的随机数情况，调整新随机数范围，以保证各份间差值在指定范围内
        if ($sum > 0) {
            $max = 0;
            $min = 0 - round($area / 2);
        } elseif ($sum < 0) {
            $min = 0;
            $max = round($area / 2);
        } else {
            $max = round($area / 2);
            $min = 0 - round($area / 2);
        }

        //产生各份的份额
        $random     = rand($min, $max);
        $sum        += $random;
        $result[$i] = $average + $random;
    }

    //最后一份的份额由前面的结果决定，以保证各份的总和为指定值
    $result[$div] = $average - $sum;

    rsort($result);

    return implode(',', $result);
}

if (!function_exists('log_actions')) {
    function log_actions($data, $type = "text")
    {
        $CI     =& get_instance();
        if($type == "json"){
            $data = json_encode($data);
        }
        $CI->logs_m->add_logs($CI->session->userdata('user_id') ?? 0,$data);
    }
}

/* End of file function_helper.php */
/* Location: ./system/helpers/function_helper.php */